package com.zpark.cz.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.zpark.cz.util.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    private static final String QUEUE_NAME = "test_work_queue";
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
// 获取连接
        Connection conn = ConnectionUtil.getConnection();
// 获得通道
        Channel channel = conn.createChannel();
// 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
/*
发送100条消息
*/
        String msg = null;
        for(int i=0; i<100; i++){
            msg = "Hello, World! " + i;
// 发送消息
            channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
            System.out.println(msg);
// 为了测试效果，可以尝试让每条消息发送有100ms的等待时长
            Thread.sleep(100);
        }
// 关闭通道
        channel.close();
// 关闭连接
        conn.close();
    }
}
